home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / hardware / cpu115 / cpu_hl.asm < prev    next >
Assembly Source File  |  1995-02-27  |  8KB  |  582 lines

  1. ; -----------------------------------------------------------------------------
  2. ; CPU_HL.ASM   CPU Type detection routine for hi-level languages Version 1.15 
  3. ;
  4. ; Too-Much-In-One-So-Don't-Get-Lost(tm) CPU/FPU feature detection library
  5. ;
  6. ; Copyright(c) 1992-95 by B-coolWare.  Written by Bobby Z.
  7. ; -----------------------------------------------------------------------------
  8. ; This is a port from CPU_TYPE.ASH - the assembler version of TMi0SDGL(tm)
  9. ; See file CPU_TYPE.ASH for extensive comments and all.
  10.  
  11.     INCLUDE    HEADER.ASH
  12.  
  13.     .DATA
  14.  
  15.     EXTRN FPUType:BYTE
  16.  
  17.     .CODE
  18.  
  19.     PUBLIC    CPU_Type
  20.     PUBLIC    checkEmu
  21. ;    PUBLIC    checkAMD
  22.  
  23. ; TP/BP:
  24. ; function CPU_Type : Word;
  25. ;
  26. ; C/C++:
  27. ; word CPU_Type(void);
  28. ;
  29. ; returns current CPU code (see CPUTYPE.PAS or CPUTYPE.H for details) and sets
  30. ; current FPU code in FPUType variable.
  31.  
  32. EF_AC        equ    00040000h    ; AC bit in EFLAGS register
  33. EF_ID        equ    00200000h    ; ID bit in EFLAGS register
  34. MSW_NE        equ    00000020h    ; NE bit in MSW register
  35.  
  36. cpuid        equ    <db 0Fh,0A2h>    ; 586 instruction
  37.  
  38. CPU_Type    proc
  39.     .8086
  40.     push    bx cx si
  41.     sub    bx,bx
  42.     push    sp
  43.     pop    ax
  44.     cmp    ax,sp
  45.     jnz    @@Ct_000
  46.     mov    ax,7000h
  47.     pushf
  48.     push    ax
  49.     popf
  50.     pushf
  51.     pop    ax
  52.     popf
  53.     mov    bl,6
  54.     and    ax,7000h
  55.     jz    @@Ct_200
  56.     inc    bx
  57.  
  58.     .386p
  59.     clr    si
  60.     mov    eax,cr0
  61.     mov    ecx,eax
  62.     xor    eax,10h
  63.     mov    cr0,eax
  64.     mov    eax,cr0
  65.     mov    cr0,ecx
  66.     xor    eax,ecx
  67.     jz    @@L100
  68.     inc    si
  69.  
  70. @@L100:
  71.  
  72.     .386
  73.  
  74.     mov    ax,sp
  75.     and    sp,0FFFCh
  76.     pushfd
  77.     pushfd
  78.     pop    edx
  79.     mov    ecx,edx
  80.     xor    edx,EF_AC
  81.     and    ecx,EF_AC
  82.     push    edx
  83.     popfd
  84.     pushfd
  85.     pop    edx
  86.     popfd
  87.     mov    sp,ax
  88.     and    edx,EF_AC
  89.     cmp    edx,ecx
  90.     jnz    @@486
  91.     or    si,si
  92.     jz    @@L1
  93.     inc    bx
  94.     jmp    @@L1
  95.  
  96. @@486:
  97.     mov    bx,0Ah
  98.     push    bx
  99.     mov    bx,100h
  100.     mov    ah,7Fh
  101.     cwd
  102.     sahf
  103.     div    bx
  104.     lahf
  105.     xor    al,ah
  106.     and    al,0D5h
  107.     pop    bx
  108.     jnz    @@586
  109.     inc    bx
  110.     inc    bx
  111.     or    si,si
  112.     jz    @@586
  113.     inc    bx
  114. @@586:
  115.  
  116.         pushfd
  117.     pop    eax
  118.     mov    ecx,eax
  119.     xor    eax,EF_ID
  120.     push    eax
  121.     popfd
  122.     pushfd
  123.     pop    eax
  124.     push    ecx
  125.     popfd
  126.     and    eax,EF_ID
  127.     and    ecx,EF_ID
  128.     cmp    eax,ecx
  129.     jz    @@486sdx
  130.     clr    eax
  131.     inc    al
  132.     push    bx
  133.     cpuid
  134.     pop    bx
  135.     and    ah,0Fh
  136.     cmp    ah,4
  137.     jb    @@386dx
  138.     je    @@486sdx2
  139.     cmp    bl,0Ch
  140.     jb    @@P5
  141.     cmp    bl,0Dh
  142.     ja    @@P5
  143.     mov    bl,0Fh
  144.     jmp    @@L1
  145. @@P5:
  146.     mov    bl,ah
  147.     add    bl,9
  148.     jmp    @@L1
  149. @@386dx:
  150.     mov    bx,7
  151.     or    si,si
  152.     jz    @@L1
  153.     inc    bx
  154.     jmp    @@L1
  155. @@486sdx2:
  156.     mov    bl,0Ah
  157.     and    al,0F0h
  158.     cmp    ax,470h
  159.     jz    @@checkP24D
  160. @@noP24:
  161.     test    al,al
  162.     jnz    @@setNibble
  163.     inc    al
  164. @@setNibble:
  165.     or    bh,al
  166.     jmp    @@L1
  167. @@checkP24D:
  168.     push    ax
  169.     clr    eax
  170.     cpuid
  171.     cmp    eax,1
  172.     pop    ax
  173.     jnz    @@noP24
  174.     cmp    ebx,756E6547h
  175.     jnz    @@noP24
  176.     cmp    ecx,6C65746Eh
  177.     jnz    @@noP24
  178.     cmp    edx,49656E69h
  179.     jnz    @@noP24
  180.     mov    bl,10h
  181.     jmp    @@L1
  182.  
  183. @@486sdx:
  184.     .486p
  185.     call    isInOSZwei
  186.     jnz    @@L1
  187.     cmp    bl,0Dh
  188.     jae    @@L1
  189.     mov    eax,cr0
  190.     mov    ecx,eax
  191.     db    66h,83h,0E0h,0DFh
  192.     mov    cr0,eax
  193.     mov    eax,cr0
  194.     cmp    eax,ecx
  195.     jnz    @@486dx
  196.     or    eax,MSW_NE
  197.     mov    cr0,eax
  198.     mov    eax,cr0
  199.     cmp    eax,ecx
  200.     jnz    @@486dx
  201.     dec    bx
  202. @@486dx:
  203.     inc    bx
  204.     mov    eax,ecx
  205.     mov    cr0,eax
  206.  
  207. @@L1:
  208.     .286p
  209.     smsw    ax
  210.     and    al,1
  211.     or    bh,al
  212.     jmp    @@Ct_200
  213. @@Ct_000:
  214.     mov    bl,4
  215.     mov    cl,33
  216.     clr    ax
  217.     dec    ax
  218.     shl    ax,cl
  219.     jnz    @@Ct_100
  220.     mov    bl,2
  221.     clr    cx
  222.     dec    cx
  223.     db    0F3h,26h,0ACh
  224.     jcxz    @@Ct_100
  225.     clr    bx
  226. @@Ct_100:
  227.     call    Test_Buffer
  228.     jcxz    @@Ct_200
  229.     inc    bx
  230. @@Ct_200:
  231.     test    ah,0F0h
  232.     jz    @@checkFPU
  233.     test    dl,1
  234.     jz    @@checkFPU
  235.     mov    dl,10h
  236.     jmp    @@noCheck
  237. @@checkFPU:
  238.     call    FPU_Type
  239. @@noCheck:
  240.     call    checkWeitek
  241.     mov    FPUType,dl
  242.     mov    ax,bx
  243.     pop    si cx bx
  244.     ret
  245.     endp
  246.  
  247.     db    13,10
  248.     db    '                Too much is not enough...',13,10
  249.     db    '                            (Deep Purple)',13,10
  250.     db    13,10
  251.     db    'TMi0SDGL(tm) CPU/FPU feature detection library  Version 1.15 ',13,10
  252.     db    'Copyright(c) 1992-95 by B-coolWare.     Released as freeware.',13,10
  253.  
  254. Test_Buffer    proc near
  255. ; this routine will never be called in DPMI applications for they won't run
  256. ; on CPU less that 286. Thus we need no precautions about writing to code
  257. ; segment.
  258.     push    es di
  259.     std
  260.     mov    _bpcs[@@0],41h
  261.     push    cs
  262.     pop    es
  263.     ldi    @@2
  264.     mov    al,_bpcs[@@1]
  265.     mov    cx,3
  266.     cli
  267.     rep    stosb
  268.     cld
  269.     nop
  270.     nop
  271.     nop
  272. @@0:    inc    cx
  273. @@1:
  274.     sti
  275. @@2:    
  276.     sti
  277.     pop    di es
  278.     ret
  279.     endp
  280.  
  281. isInOSZwei    proc    near
  282.  
  283.     push    ax bx
  284.     mov    ax,4010h
  285.     int    2Fh
  286.     cmp    ax,4010h
  287.     pop    bx ax
  288.     ret
  289.     endp
  290.  
  291. checkWeitek    proc    near
  292.     cmp    bl,7
  293.     jb    @@1
  294.     .386
  295.     clr    eax
  296.     int    11h
  297.     test    eax,1000000h
  298.     .8086
  299.     jz    @@1
  300.     or    dl,1
  301. @@1:
  302.     ret
  303.     endp
  304.  
  305. fsbp0    equ    <db 0DBh,0E8h>
  306. fsbp1    equ    <db 0DBh,0EBh>
  307. fsbp2    equ    <db 0DBh,0EAh>
  308. fmul4x4    equ    <db 0DBh,0F1h>
  309.  
  310.  
  311. FPU_Type    proc near
  312.     .8086
  313.     .8087
  314.     push    ds
  315. ifdef    __DPMI__
  316.     push    ax bx
  317.     mov    ax,dpmiCreateCodeAlias
  318.     mov    bx,cs
  319.     int    31h
  320.     mov    ds,ax
  321.     pop    bx ax
  322. else
  323.     push    cs
  324.     pop    ds
  325.     ASSUME  DS:CPU_HL_TEXT
  326. endif
  327.     mov    dl,2
  328.     fninit
  329.     xor    cx,cx
  330.     jmp    $+2
  331.     mov    ds:fpudata1,5A5Ah
  332.     fnstsw    ds:fpudata1
  333.     mov    ax,ds:fpudata1
  334.     or    al,al
  335.     jnz    @@L15
  336.     fnstcw    ds:fpudata1
  337.     mov    ax,ds:fpudata1
  338.     and    ax,103Fh
  339.     cmp    ax,3Fh
  340.     jne    @@L15
  341.     mov    dl,4
  342.     fstenv    ds:fpudata3
  343.     fwait
  344.     and    ds:fpudata1,0FF7Fh
  345.     fldcw    ds:fpudata1
  346.     fwait
  347.     fdisi
  348.     fstcw    ds:fpudata1
  349.     fwait
  350.     test    ds:fpudata1,80h
  351.     jnz    @@L15
  352.     mov    dl,8
  353.     .286
  354.     .287
  355.     fninit
  356.     fld1
  357.     fldz
  358.     fdivp    st(1),st
  359.     fld    st
  360.     fchs
  361.     fcompp
  362.     fstsw    ds:fpudata1
  363.     fwait
  364.     mov    ax,ds:fpudata1
  365.     sahf
  366.     jz    @@L14
  367.     mov    dl,0Ch
  368. @@L14:
  369.     .286
  370.     .287
  371.  
  372.     cmp    bl,09h
  373.     jb    @@checkIIT
  374.     jmp    @@50
  375.  
  376. @@checkIIT:
  377.  
  378.     finit
  379.     fsbp1
  380.     wait
  381.     fldz
  382.     fld1
  383.     fldz
  384.     fldz
  385.     fld1
  386.     fldz
  387.     fldz
  388.     fldz
  389.     wait
  390.     finit
  391.     fsbp2
  392.     wait
  393.     fldz
  394.     fldz
  395.     fldz
  396.     fld1
  397.     fldz
  398.     fldz
  399.     fld1
  400.     fldz
  401.     wait
  402.     finit
  403.     fsbp0
  404.     wait
  405.     fldz
  406.     fld1
  407.     fld    st(0)
  408.     fadd    st,st(0)
  409.     fld    st(0)
  410.     fadd    st,st(2)
  411.     fmul4x4
  412.     wait
  413.     fstp    ds:iit1
  414.     fstp    ds:iit2
  415.     fstp    ds:iit3
  416.     fstp    ds:iit4
  417.     wait
  418.     cmp    _wp ds:[iit4+2],4040h
  419.     jnz    @@50
  420.     cmp    _wp ds:[iit3+2],4000h
  421.     jnz    @@50
  422.     cmp    _wp ds:[iit2+2],3F80h
  423.     jnz    @@50
  424.     cmp    _wp ds:[iit1],0
  425.     jnz    @@50
  426.     cmp    dl,0Ch
  427.     jz    @@300
  428.     mov    dl,22
  429.     jmp    @@L15
  430. @@300:
  431.     mov    dl,24
  432.     jmp    @@L15
  433. @@50:
  434.     finit
  435.     fldpi
  436.     f2xm1
  437.     fstp    ds:fpudata2
  438.     cmp    _wp ds:[fpudata2+2],3FC9h
  439.     jne    @@L15
  440.     or    dl,2
  441. @@L15:
  442.     cmp    bl,0Eh
  443.     jb    @@17
  444.     and    dl,3
  445.     or    dl,10h
  446.     jmp    @@30
  447. @@17:
  448.     cmp    bl,0Bh
  449.     jnz    @@L17
  450.     cmp    dl,3
  451.     ja    @@builtin
  452.     dec    bl
  453.     jmp    @@31
  454. @@builtin:
  455.     and    dl,1
  456.     or    dl,10h
  457.     jmp    @@nobuilt
  458. @@L17:
  459.     cmp    bl,0Eh
  460.     jae    @@builtin
  461.     cmp    bl,0Dh
  462.     jnz    @@nobuilt
  463.     cmp    dl,12h
  464.     jz    @@builtin
  465.     cmp    dl,13h
  466.     jz    @@builtin
  467. @@nobuilt:
  468.     cmp    bl,06
  469.     jnz    @@30
  470.     cmp    dl,0Ch
  471.     jz    @@XL
  472.     cmp    dl,0Dh
  473.     jnz    @@30
  474. @@XL:
  475.     add    dl,8
  476. @@30:
  477.     cmp    bl,0Ch
  478.     jz    @@is387
  479.     cmp    bl,0Ah
  480.     jnz    @@31
  481. @@is387:
  482.     cmp    dl,0Ch
  483.     jz    @@487sx
  484.     cmp    dl,0Dh
  485.     jnz    @@31
  486. @@487sx:
  487.     and    dl,1
  488.     or    dl,6    
  489. @@31:
  490.     cmp    dl,4
  491.     jb    @@L18
  492.     fldenv    ds:fpudata3
  493. @@L18:
  494. ifdef    __DPMI__
  495.     push    ax bx
  496.     mov    ax,dpmiFreeLDTDesc
  497.     mov    bx,ds
  498.     int    31h
  499.     pop    bx ax
  500. endif
  501.     pop    ds
  502.     ret
  503.     endp
  504. fpudata1    dw    ?
  505. fpudata2    dd    ?
  506. fpudata3    db    14 dup(?)
  507.  
  508. iit1        dd    6F772049h
  509. iit2        dd    7265646Eh
  510. iit3        dd    20666920h
  511. iit4        dd    00544949h
  512.  
  513. checkEmu    proc
  514.     .286p
  515.     smsw    ax
  516.     test    al,04
  517.     mov    al,1
  518.     jnz    @@1
  519.     clr    al
  520. @@1:
  521.     ret
  522.     endp
  523.  
  524. ;checkAMD    proc
  525. ;IFNDEF    __DPMI__
  526. ;    push    ds
  527. ;    mov    ax,3506h
  528. ;    int    21h
  529. ;    push    cs
  530. ;    pop    ds
  531. ;    mov    dx,offset @@int6Trap
  532. ;    mov    ax,2506h
  533. ;    int    21h
  534. ;    sub    cx,cx
  535. ;    db    0F0h,90h    ; LOCK NOP causes INT 6 on AMD chips,
  536. ;                ; though it works ok in Intel ones.
  537. ;@@afterTrap:
  538. ;    push    es
  539. ;    pop    ds
  540. ;    mov    dx,bx
  541. ;    mov    ax,2506h
  542. ;    int    21h
  543. ;    mov    ax,cx
  544. ;    pop    ds
  545. ;    ret
  546. ;@@int6Trap:
  547. ;    add    sp,4
  548. ;    popf
  549. ;    inc    cx
  550. ;    jmp    @@afterTrap
  551. ;ELSE
  552. ;    mov    ax,dpmiGetExceptionHnd
  553. ;    mov    bl,06h
  554. ;    int    31h
  555. ;    push    cx dx
  556. ;    mov    ax,dpmiSetExceptionHnd
  557. ;    mov    bl,06h
  558. ;    push    cs
  559. ;    pop    cx
  560. ;    mov    dx,offset @@int6Trap
  561. ;    int    31h
  562. ;    mov    si,1
  563. ;    db    0F0h,90h
  564. ;    dec    si
  565. ;@@afterTrap:
  566. ;    pop    dx cx
  567. ;    mov    ax,dpmiSetExceptionHnd
  568. ;    mov    bl,06h
  569. ;    int    31h
  570. ;    mov    ax,si
  571. ;    ret
  572. ;@@int6Trap:
  573. ;    push    bp
  574. ;    mov    bp,sp
  575. ;    mov    word ptr ss:[bp+8],offset @@afterTrap    ; modify IP on stack
  576. ;    pop    bp
  577. ;    retf
  578. ;ENDIF
  579. ;    endp
  580.  
  581.     END
  582.